package client_service_controller;

import client_listen_update_files.FolderListener;
import client_listen_update_files.GetUpdatedFiles;

/**
 * Execute commands of the client service
 * 
 * --Singleton--
 * 
 * @author Orit and Yoav
 *
 */
public class Executor {
	private final String serverAddress="http://localhost:9991";
	private String linkAddress;
	private GetUpdatedFiles getUpdatedFiles=null;
	private FolderListener folderListener=null;
	private boolean running;
	
	/*
	 * private C'tor initialize listeners before start the service  
	 */
	private Executor() {		
		linkAddress=serverAddress+"/link";		
		getUpdatedFiles=new GetUpdatedFiles(linkAddress);
		running=false;
		folderListener=new FolderListener();
		
	}
	
	//--Singelton Holder--//
	private static class ExecutorHolder{ 
		private final static Executor INSTANCE = new Executor();
	}
	/**
	 * get Singelton instance of the Executor 
	 * @return Executor instance
	 */
	public static Executor getInstance(){
	    return ExecutorHolder.INSTANCE;
	}
	
	//--methods--//
	/**
	 * stop the Service -  stop update files and folder listeners 
	 */
	public void stopService(){
		if (running){
			getUpdatedFiles.stop();
			folderListener.stop();
		}
		running=false;
	}

	/**
	 * start the Service -  start update files and folder listeners 
	 * need user name && password 
	 * @param userName that want to start the service
	 * @param password of the user
	 */
	public void startService(String userName,String password){
		if (!running){
			running=true;
			new Thread(getUpdatedFiles).start();			
			new Thread (folderListener).start();	
		}
	}
}
